package carassius.GUI;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import org.apache.commons.lang.NotImplementedException;

/**
 *
 * @author siebz0r
 */
public abstract class ListWithMenu
		extends JList
{
	final JPopupMenu POPUP_MENU;

	public ListWithMenu()
	{
		ResourceBundle resourceBundle = ResourceBundle.getBundle("carassius.GUI.resources.ListWithMenu");

		POPUP_MENU = new JPopupMenu();

		JMenuItem addMenuItem = new JMenuItem();
		addMenuItem.setAction(new addAction());
		addMenuItem.setText(resourceBundle.getString("Add"));
		addMenuItem.setFont(addMenuItem.getFont().deriveFont(Font.PLAIN));
		POPUP_MENU.add(addMenuItem);

		JMenuItem removeMenuItem = new JMenuItem();
		removeMenuItem.setAction(new removeAction());
		removeMenuItem.setText(resourceBundle.getString("Remove"));
		removeMenuItem.setFont(removeMenuItem.getFont().deriveFont(Font.PLAIN));
		POPUP_MENU.add(removeMenuItem);

		JMenuItem editMenuItem = new JMenuItem();
		editMenuItem.setAction(new editAction());
		editMenuItem.setText(resourceBundle.getString("Edit"));
		editMenuItem.setFont(editMenuItem.getFont().deriveFont(Font.PLAIN));
		POPUP_MENU.add(editMenuItem);
	}

	public ListWithMenu(JPopupMenu popupMenu)
	{
		POPUP_MENU = popupMenu;
	}

	abstract void add();

	abstract void remove();

	abstract void edit();

	private void showPopupMenu(boolean objectSelected)
	{
		ResourceBundle resourceBundle = ResourceBundle.getBundle("carassius.GUI.resources.ListWithMenu");
		for (Component c : POPUP_MENU.getComponents())
		{
			if (c instanceof JMenuItem)
			{
				if ((((JMenuItem) c).getText().equals(resourceBundle.getString("Remove")))
						|| ((JMenuItem) c).getText().equals(resourceBundle.getString("Edit")))
				{
					c.setEnabled(objectSelected);
				}
			}
		}
		POPUP_MENU.show(this, getMousePosition().x, getMousePosition().y);
	}

	@Override
	protected void processMouseEvent(MouseEvent e)
	{
		if (e.getID() == MouseEvent.MOUSE_CLICKED)
		{
			switch (e.getButton())
			{
				case MouseEvent.BUTTON1:
					if (e.getClickCount() == 2)
					{
						edit();
					}
					break;
				case MouseEvent.BUTTON3:
					showPopupMenu(getSelectedValue() != null);
					break;
			}

		}
		super.processMouseEvent(e);
	}

	@Override
	protected void processKeyEvent(KeyEvent e)
	{
		switch (e.getKeyCode())
		{
			case KeyEvent.VK_DELETE:
				remove();
				break;
			case KeyEvent.VK_ENTER:
				edit();
				break;
			case KeyEvent.VK_ADD:
				add();
				break;
			case KeyEvent.VK_ESCAPE:
				clearSelection();
				break;
		}
		super.processKeyEvent(e);
	}

	class addAction
			extends AbstractAction
			implements Action
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			add();
		}
	}

	class removeAction
			extends AbstractAction
			implements Action
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			remove();
		}
	}

	class editAction
			extends AbstractAction
			implements Action
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			edit();
		}
	}
}
